package com.homestead.client;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;


public class Client {
    public static void main(String[] args) {

        KeyPair keyPair = SecureUtil.generateKeyPair("RSA");
        String publicKey = HexUtil.encodeHexStr(keyPair.getPublic().getEncoded(), false);
        String privateKey = HexUtil.encodeHexStr(keyPair.getPrivate().getEncoded(), false);
        System.out.println("publicKey = " + publicKey);
        System.out.println("privateKey = " + privateKey);

        RSA rsa = new RSA(privateKey, publicKey);

        byte[] encrypt = rsa.encrypt("12398901", KeyType.PublicKey);
        String encStr = HexUtil.encodeHexStr(encrypt, false);
        System.out.println("encStr = " + encStr);
        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);

        String str = StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
        System.out.println("str = " + str);
    }

}
